package com.newtribe.access;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.security.KeyStore;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;

public class SSLClient {


 public static Socket getSslSocket(String host ,int port) throws Exception{
	

	 String trustfile ="ssl.keystore" ;
	char keyStorePass[]="111111".toCharArray();  //֤������
	char keyPassword[]="111111".toCharArray();  //֤������ʹ�õ���Ҫ����

	 KeyStore ks=KeyStore.getInstance("JKS");  //����JKS��Կ��

	 ks.load(new FileInputStream(trustfile),keyStorePass);
     KeyManagerFactory kmf=KeyManagerFactory.getInstance("SunX509");
	TrustManagerFactory trust =TrustManagerFactory.getInstance("SunX509") ;

	 kmf.init(ks,keyPassword);
	 trust.init(ks);

	SSLContext sslContext=SSLContext.getInstance("SSLv3");
    sslContext.init(null ,trust.getTrustManagers(),null);
    SSLSocketFactory factory = (SSLSocketFactory) sslContext.getSocketFactory() ;
	Socket s = factory.createSocket(host,port);
	return s ;

		
 }
  public static void main(String [] argvs)throws Exception{
	  Socket s =SSLClient.getSslSocket("www.verisign.com",443);
      PrintWriter out = new PrintWriter(s.getOutputStream(), true);
      out.println("GET www/ HTTP/1.1\r\n\r\n");
      out.close();

      InputStream in = s.getInputStream() ;
      byte[] buffer =new byte[2048] ;
      int c =in.read(buffer) ;
      System.out.println(new String(buffer,0,c) );

      s.close();
  }
}
